import { observable, action, toJS } from 'mobx';
import { Bubble, Tools } from 'weaver-mobile';
import { ListViewPage } from 'weaver-mobile-page';
import * as Api from '../../api/BlogAttentionApi'
const getLabel = Tools.getLabel;

const initalState = {
  searchValue: '',
  myFansNoData: false,
}
export default class MyFansStore {
  @observable myFansList = new ListViewPage.Store({ route:'/userCenter/myFans', id:'myFans' });
  @observable searchValue = initalState.searchValue;
  @observable myFansNoData = initalState.myFansNoData;

  @action setState = (params = {}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
  }

  @action('我的粉丝列表请求')
  getMyFansList = () => {
    let searchParams = { searchKey: this.searchValue }
    Api.getMyAttentionMesUsers(searchParams).then(data=>{
      const { attnList, promptMsg } = data;
      if (promptMsg) {
        this.myFansNoData = true 
      } else {
        const newState = {
          isTable: false,
          loading: false,
          haveCheck: true,
          template: {default: []},
          hasMore: false,
          data: [...attnList],
        };
        this.myFansNoData = false;
        this.myFansList.setState(newState);
        this.myFansList.updateDataSource();
      }
    })
  }

  @action('我的粉丝-搜索')
  myFansSearch = (v) => {
    this.searchValue = v;
    this.getMyFansList();
  }

  @action('更新我的粉丝列表信息')
  updateFansAttentionStatus = (attnInfo) =>{
    let {userId,attentionMeStatus} = attnInfo;
		let attlist = toJS(this.myFansList.data).map(att=>{
				if (att.userId === userId)
          return {...att, attentionMeStatus: attentionMeStatus};
				return att;
		});
    this.myFansList.setState({data: attlist});
    this.myFansList.updateDataSource();
  }
  
  @action('粉丝操作')
  attentionMeOpt = (params, callback) => {
    Api.attentionMeOpt(params).then(result=>{
      if (result.msg) Bubble.warning(getLabel(384418,'已发送申请！') , 1);
      else if (callback) callback(result.attnInfo)
    })
  }

  @action('组件卸载操作')
  myFansClear = () => this.setState(initalState);
}